package tedu.week04.day02_thread.lesson3;

/**
 * <pre>
 *      线程的同步.
 *          火车票示例:
 * </pre>
 *
 * @author : resty
 * @date : 2019年07月09日    10时
 **/
public class SellThread implements Runnable {
    private int ticket = 5 ;
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            ticket();
        }
    }

    public synchronized void ticket(){
        if (ticket>0){
            try {
                Thread.sleep(300);//网络延迟
            } catch (InterruptedException e) {

            }
            System.out.println("ticket:"+ticket--+"张");
        }
    }
    public static void main(String[] args){
        SellThread st = new SellThread();
        Thread t1 = new Thread(st);
        Thread t2 = new Thread(st);
        Thread t3 = new Thread(st);
        t1.start();
        t2.start();
        t3.start();
    }
}
